import requests
from jsonpath import jsonpath
import json
import sys
from openpyxl import workbook

wb = workbook.Workbook()   # 创建excel对象
ws = wb.active   # 激活表对象
ws.append(['省份', '市级', '区级', '名字'])

url = "https://www.leapmotor.com/StoreDrainage/getStoreLongitudeLatitude"

payload={}
headers = {
  'authority': 'www.leapmotor.com',
  'accept': 'application/json, text/plain, */*',
  'accept-language': 'zh,zh-CN;q=0.9,en;q=0.8',
  'cookie': 'Hm_lvt_a28ebe6ef2b8ff145798557bcbbf81ac=1649601273; Hm_lpvt_a28ebe6ef2b8ff145798557bcbbf81ac=1649601342',
  'referer': 'https://www.leapmotor.com/expreience/expreienceStore.html',
  'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
  'sec-ch-ua-mobile': '?0',
  'sec-ch-ua-platform': '"Windows"',
  'sec-fetch-dest': 'empty',
  'sec-fetch-mode': 'cors',
  'sec-fetch-site': 'same-origin',
  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'
}

response = requests.request("GET", url, headers=headers, data=payload)
r = response.text
print(type(r))
p = json.loads(r)
print(type(p))


# $ 表示根节点   ..表示跳过任意层级
shenfen = jsonpath(p,"$..areaShopProvince")
# print(shenfen)
shiji = jsonpath(p,"$..areaShopCity")
quji = jsonpath(p,"$..areaShopDistrict")
name = jsonpath(p,"$..name")
for a,b,c,d in zip(shenfen,shiji,quji,name):
    # print(a)
    # print(b)
    # print(c)
    # print(d)
    # print("="*10)
    my_list = [a,b,c,d]   # 以列表形式写入数据
    ws.append(my_list)
    wb.save("零跑.xlsx")






















